package com.cc.app.ui;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.RectEvaluator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

/**
 *
 */
public class MainView extends View implements View.OnTouchListener {

    private List<Bitmap> list = new ArrayList<>();

    private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);

    private int gap = 20;

    private boolean isMoving = false;

    private int mode = 0;

    private List<RectF> buttons = new ArrayList<>();

    private int currentIndex = 0;

    private Rect leftRect;

    private Rect centerRect;

    private Rect rightRect;

    private Rect lastRect;

    private int[] weights = new int[]{1, 4, 1};

    private boolean loop = false;

    private int half = 0;

    public MainView(Context context, AttributeSet attr) {
        super(context, attr);
        setOnTouchListener(this);
    }

    public void changeMode(int mode) {
        this.mode = mode;
    }

    public void addImage(Bitmap bitmap) {
        this.list.add(bitmap);
    }

    public void setWeights(int[] weights) {
        if (weights == null || weights.length != 3)
            return;

        this.weights = weights;
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            action(event);
        }

        return true;
    }

    private void action(MotionEvent event) {
        int x = (int) event.getX();
        int y = (int) event.getY();

        loop = !loop;

        if (isMoving)
            return;

        move();
    }

    private void initRects() {
        int width = getWidth() - (weights.length + 1) * gap;
        int height = getHeight() - gap * 2;

        int sum = 0;
        for (int e : weights) {
            sum += e;
        }

        int leftWidth = width * weights[0] / sum;
        int rightWidt = width * weights[2] / sum;
        int centerWidth = width - leftWidth - rightWidt;

        half = gap + height / 4;

        leftRect = new Rect(gap, half, gap + leftWidth, half + height / 2);

        centerRect = new Rect(leftRect.right + gap, gap, leftRect.right + gap + centerWidth, gap + height);

        rightRect = new Rect(centerRect.right + gap, half, centerRect.right + gap + rightWidt, half + height / 2);

        lastRect = new Rect(rightRect.right + gap, half, rightRect.right + gap + rightWidt, half + height / 2);
    }

    private void move() {
        if (!loop)
            return;

        initRects();

        Rect blank = new Rect(leftRect.left - leftRect.width(), half, leftRect.right - leftRect.width() - gap, half + leftRect.height() / 2);
        ValueAnimator leftAnimator = ValueAnimator.ofObject(new RectEvaluator(leftRect), leftRect, blank);
        ValueAnimator centerAnimator = ValueAnimator.ofObject(new RectEvaluator(centerRect), centerRect, new Rect(leftRect));
        ValueAnimator rightAnimator = ValueAnimator.ofObject(new RectEvaluator(rightRect), rightRect, new Rect(centerRect));
        ValueAnimator lastAnimator = ValueAnimator.ofObject(new RectEvaluator(lastRect), lastRect, new Rect(rightRect));

        AnimatorSet as = new AnimatorSet();
        as.play(leftAnimator).with(centerAnimator).with(rightAnimator).with(lastAnimator);
        as.setDuration(2000);

        ValueAnimator.AnimatorUpdateListener listener = new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                invalidate();
            }
        };

        leftAnimator.addUpdateListener(listener);

        as.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                isMoving = false;
                currentIndex = (++currentIndex) % list.size();
                move();
            }

            @Override
            public void onAnimationStart(Animator animation) {
                super.onAnimationStart(animation);
                isMoving = true;
            }
        });
        as.start();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        if (list == null || list.isEmpty())
            return;

        if (list.size() == 1) {
            return;
        }


        int leftIndex = currentIndex == 0 ? list.size() - 1 : currentIndex - 1;
        int rightIndex = currentIndex == list.size() - 1 ? 0 : currentIndex + 1;
        int lastIndex = rightIndex == list.size() - 1 ? 0 : rightIndex + 1;

        Bitmap left = list.get(leftIndex);
        Bitmap center = list.get(currentIndex);
        Bitmap right = list.get(rightIndex);
        Bitmap last = list.get(lastIndex);

        if (leftRect == null) {
            canvas.drawBitmap(center, null, new Rect(gap, gap, getWidth() - gap * 2, getHeight() - gap * 2), paint);
            return;
        }
        canvas.drawBitmap(left, null, leftRect, paint);
        canvas.drawBitmap(center, null, centerRect, paint);
        canvas.drawBitmap(right, null, rightRect, paint);
        canvas.drawBitmap(last, null, lastRect, paint);

//        canvas.scale(0.5f,0.5f);

//        if (row == selectRow && col == selectCol && p2 != null) {
//            rect = new RectF(p2.x + gap, p2.y + gap, p2.x + width - gap * 2, p2.y + hegith - gap * 2);
//        } else if (row == blankRow && col == blankCol && p1 != null) {
//            rect = new RectF(p1.x + gap, p1.y + gap, p1.x + width - gap * 2, p1.y + hegith - gap * 2);
//        }

    }
}
